package com.example.myapplication.presenter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;

import com.example.myapplication.api.ApiConstants;
import com.example.myapplication.api.HostType;
import com.example.myapplication.base.RxPresenter;
import com.example.myapplication.model.bean.NewsSummary;
import com.example.myapplication.model.https.RetrofitHelpers;
import com.example.myapplication.model.rx.RxSubscriber;
import com.example.myapplication.presenter.contract.NewContract;
import com.example.myapplication.ui.wight.LoadingDialog;
import com.example.myapplication.utils.TimeUtil;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by meishiyiyun on 2017/5/27.
 */

public class NewPresenter extends RxPresenter<NewContract.View> implements NewContract.Presenter {

    private  Context mContext;
    public NewPresenter(Context context) {
        this.mContext=context;
    }

    @Override
    public void requestDataRefresh(String NewsType, String NwsIed, int page) {
        Dialog  dialog= LoadingDialog.showDialogForLoading((Activity) mContext, "加载中...",false,false);
        getNewsListData(NewsType,NwsIed,page).subscribe(new RxSubscriber<List<NewsSummary>>(dialog) {
            @Override
            protected void _onNext(List<NewsSummary> newsSummaries) {
            mView.SuccessRefresh(newsSummaries);
                dialog.dismiss();
            }

            @Override
            protected void _onError(String message) {
                mView.showError(message);
            }
        });
    }

    @Override
    public void requestDataLoadMore(String NewsType, String NwsIed, int page) {
        Dialog  dialog= LoadingDialog.showDialogForLoading((Activity) mContext, "加载中...",false,false);
        getNewsListData(NewsType,NwsIed,page).subscribe(new RxSubscriber<List<NewsSummary>>(dialog) {
                @Override
            protected void _onNext(List<NewsSummary> newsSummaries) {
                mView.SuccessLoadMore(newsSummaries);   dialog.dismiss();
            }
            @Override
            protected void _onError(String message) {
                mView.showError(message);
            }
        });
    }
    private Observable<List<NewsSummary>> getNewsListData(String NewsType, String NwsIed, int page){
        return RetrofitHelpers.getInstance(HostType.NETEASE_NEWS_VIDEO,false).getNewsList(NewsType,NwsIed,page)
                .flatMap(stringListMap -> {
                    if (NwsIed.endsWith(ApiConstants.HOUSE_ID)) {
                        return Observable.fromIterable(stringListMap.get("北京"));   // 房产实际上针对地区的它的id与返回key不同
                    }
                    return Observable.fromIterable(stringListMap.get(NwsIed));
                }).map(newsSummary -> {
                    String ptime = TimeUtil.formatDate(newsSummary.getPtime());
                    newsSummary.setPtime(ptime);
                    return newsSummary;
                }).distinct()
                .toSortedList((o1, o2) -> o2.getPtime().compareTo(o1.getPtime()))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).toObservable();
    }
}
